package com.tebiecloud.act.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.act.client.model.entity.Act;
import com.tebiecloud.act.server.service.ActService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 活动表
 前端控制器
 * @author tebie
 * @date 2019-10-09
 */
@RestController
@RequestMapping("/act")
public class ActController {

    @Autowired
    private ActService actService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<Act>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        Act query = pageParams.mapToObject(Act.class);
        QueryWrapper<Act> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(actService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<Act> get(@RequestParam("id") Long id) {
        Act entity = actService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param storeId 门店id,
     * @param name 活动名称,
     * @param actType 活动类型：1限时活动，2体验活动,
     * @param mainPic 主图,
     * @param shelfTime 上架时间,
     * @param startTime 开售时间,
     * @param endTime 结束时间,
     * @param codeStime 有效开始时间,
     * @param codeEtime 有效结束时间,
     * @param carouselPics 轮播图,
     * @param detailPics 详情图,
     * @param sharePic 分享图片,
     * @param shareTitle 分享标题,
     * @param shareRemark 分享描述,
     * @param isRefundable 是否可以退款：0否，1是,
     * @param purchaseQty 单人可购买数量,
     * @param effectiveDay 有效天数,
     * @param effectiveType 有效类型：0指定日期有效，1购买后有效天数，2长期有效,
     * @param des 使用规则,
     * @param status 启用状态：0、停用，1、启用,
     * @param deleted 删除状态：0、未删除，1、已删除
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "storeId", required = false) Long storeId,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "actType", required = false) Integer actType,
        @RequestParam(value = "mainPic", required = false) String mainPic,
        @RequestParam(value = "shelfTime", required = false) Date shelfTime,
        @RequestParam(value = "startTime", required = false) Date startTime,
        @RequestParam(value = "endTime", required = false) Date endTime,
        @RequestParam(value = "codeStime", required = false) Date codeStime,
        @RequestParam(value = "codeEtime", required = false) Date codeEtime,
        @RequestParam(value = "carouselPics", required = false) String carouselPics,
        @RequestParam(value = "detailPics", required = false) String detailPics,
        @RequestParam(value = "sharePic", required = false) String sharePic,
        @RequestParam(value = "shareTitle", required = false) String shareTitle,
        @RequestParam(value = "shareRemark", required = false) String shareRemark,
        @RequestParam(value = "isRefundable", required = false) Integer isRefundable,
        @RequestParam(value = "purchaseQty", required = false) Integer purchaseQty,
        @RequestParam(value = "effectiveDay", required = false) Integer effectiveDay,
        @RequestParam(value = "effectiveType", required = false) Integer effectiveType,
        @RequestParam(value = "des", required = false) String des,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        Act entity = new Act();
        entity.setStoreId(storeId);
        entity.setName(name);
        entity.setActType(actType);
        entity.setMainPic(mainPic);
        entity.setShelfTime(shelfTime);
        entity.setStartTime(startTime);
        entity.setEndTime(endTime);
        entity.setCodeStime(codeStime);
        entity.setCodeEtime(codeEtime);
        entity.setCarouselPics(carouselPics);
        entity.setDetailPics(detailPics);
        entity.setSharePic(sharePic);
        entity.setShareTitle(shareTitle);
        entity.setShareRemark(shareRemark);
        entity.setIsRefundable(isRefundable);
        entity.setPurchaseQty(purchaseQty);
        entity.setEffectiveDay(effectiveDay);
        entity.setEffectiveType(effectiveType);
        entity.setDes(des);
        entity.setStatus(status);
        entity.setDeleted(deleted);
        actService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param storeId 门店id
     * @param name 活动名称
     * @param actType 活动类型：1限时活动，2体验活动
     * @param mainPic 主图
     * @param shelfTime 上架时间
     * @param startTime 开售时间
     * @param endTime 结束时间
     * @param codeStime 有效开始时间
     * @param codeEtime 有效结束时间
     * @param carouselPics 轮播图
     * @param detailPics 详情图
     * @param sharePic 分享图片
     * @param shareTitle 分享标题
     * @param shareRemark 分享描述
     * @param isRefundable 是否可以退款：0否，1是
     * @param purchaseQty 单人可购买数量
     * @param effectiveDay 有效天数
     * @param effectiveType 有效类型：0指定日期有效，1购买后有效天数，2长期有效
     * @param des 使用规则
     * @param status 启用状态：0、停用，1、启用
     * @param deleted 删除状态：0、未删除，1、已删除
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "actId", required = false) Long actId,
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "actType", required = false) Integer actType,
            @RequestParam(value = "mainPic", required = false) String mainPic,
            @RequestParam(value = "shelfTime", required = false) Date shelfTime,
            @RequestParam(value = "startTime", required = false) Date startTime,
            @RequestParam(value = "endTime", required = false) Date endTime,
            @RequestParam(value = "codeStime", required = false) Date codeStime,
            @RequestParam(value = "codeEtime", required = false) Date codeEtime,
            @RequestParam(value = "carouselPics", required = false) String carouselPics,
            @RequestParam(value = "detailPics", required = false) String detailPics,
            @RequestParam(value = "sharePic", required = false) String sharePic,
            @RequestParam(value = "shareTitle", required = false) String shareTitle,
            @RequestParam(value = "shareRemark", required = false) String shareRemark,
            @RequestParam(value = "isRefundable", required = false) Integer isRefundable,
            @RequestParam(value = "purchaseQty", required = false) Integer purchaseQty,
            @RequestParam(value = "effectiveDay", required = false) Integer effectiveDay,
            @RequestParam(value = "effectiveType", required = false) Integer effectiveType,
            @RequestParam(value = "des", required = false) String des,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        Act entity = new Act();
        entity.setActId(actId);
        entity.setStoreId(storeId);
        entity.setName(name);
        entity.setActType(actType);
        entity.setMainPic(mainPic);
        entity.setShelfTime(shelfTime);
        entity.setStartTime(startTime);
        entity.setEndTime(endTime);
        entity.setCodeStime(codeStime);
        entity.setCodeEtime(codeEtime);
        entity.setCarouselPics(carouselPics);
        entity.setDetailPics(detailPics);
        entity.setSharePic(sharePic);
        entity.setShareTitle(shareTitle);
        entity.setShareRemark(shareRemark);
        entity.setIsRefundable(isRefundable);
        entity.setPurchaseQty(purchaseQty);
        entity.setEffectiveDay(effectiveDay);
        entity.setEffectiveType(effectiveType);
        entity.setDes(des);
        entity.setStatus(status);
        entity.setDeleted(deleted);
        actService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            actService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            actService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
